package cn.biplam.front.component.view.systemManage.sysprivi.privilege.menu.manage.add;

import cn.biplam.common.ajxcode.SystemCode.MenuCode;
import cn.biplam.common.bean.md.IndexTable;
import cn.biplam.common.bean.systemManage.privi.MenuManageBean;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.MenuOptCode;
import cn.biplam.common.paramters.TreeParam;
import cn.biplam.common.struc.SysStruc.MenuStruc;
import cn.biplam.common.struc.SysStruc.PriviStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.ComponentBase;
import cn.biplam.front.component.view.systemManage.sysprivi.privilege.menu.navi.MenuNavi;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.htmlscript.script.ScriptTreeNew;
import cn.biplam.front.template.base.Input;
import cn.biplam.front.template.base.TextArea;
import cn.biplam.front.template.complex.InputPrompt;

/**
 * 新建菜单页面
 * @author XiaoRuxing Dec 8, 20098:47:21 PM
 * @version 1.0
 */
public class MenuAdd extends ComponentBase{

	private static final String cId = MenuCode.Add_Menu;

	public MenuAdd(UserInfo user, String homeUrl, String recordId,
			KvMap<String, String> params) {
		super(user, homeUrl, recordId, params);
	}

	/**
	 * @return 添加菜单界面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow get() throws ErrorMessage{
		String cPageId = cId+".add";
		//菜单ID
		this.recordId = params.getByKey(TreeParam.NODE_ID);
		//菜单父亲操作类型
		IndexTable table = IndexTable.getNewInstance(mUser.getUserID(), PriviStruc.Table_Name,recordId);
		String operatorType = table.getOperatorType();
		//添加菜单界面
		InputPrompt addInput = new InputPrompt(MenuCode.Add_Menu_Submit,recordId,cPageId);
		addInput.putParam(TreeParam.OPERATOR_TYPE, operatorType);
		addInput.putParam(TreeParam.NODE_CONTARINER_ID, 
				params.getByKey(TreeParam.NODE_CONTARINER_ID));
		HtmlWindow add = panel(mUser, sHomeUrl, recordId, cPageId, addInput, false);
		return add;
	}
	
	/**
	 * @return 添加菜单界面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow getEdit() throws ErrorMessage{
		String cPageId = cId+".edit";
		//菜单ID
		this.recordId = params.getByKey(TreeParam.NODE_ID);
		//修改菜单界面
		InputPrompt editInput = new InputPrompt(MenuCode.Edit_Menu_Submit,recordId,cPageId);
		editInput.putParam(TreeParam.NODE_CONTARINER_ID, 
				params.getByKey(TreeParam.NODE_CONTARINER_ID));
		HtmlWindow edit = panel(mUser, sHomeUrl, recordId, cPageId, editInput,true);
		return edit;
	}
	
	/**
	 *  添加菜单操作
	 * @return 刷新脚本
	 * @throws ErrorMessage 
	 */
	public HtmlWindow submit() throws ErrorMessage{
		//添加菜单操作
		MenuManageBean menuOpt = MenuManageBean.getInstance(mUser.getUserID());
		String parentId = recordId;
		String parentOpType = params.getByKey(TreeParam.OPERATOR_TYPE);
		String menuName = params.getByKey(MenuStruc.MENU_NAME);
		String menuDesc = params.getByKey(MenuStruc.MENU_DESC);
		String menuPicPath = params.getByKey(MenuStruc.PIC_PATH);
		String menuActionId = params.getByKey(MenuStruc.ACTION_ID);
		OperatorResult rs = menuOpt.insert(parentId, parentOpType, menuName, menuDesc, menuPicPath, menuActionId);
		//添加后刷新菜单管理树
		String script = ScriptTreeNew.getRefreshAdd(params.getByKey(TreeParam.NODE_CONTARINER_ID), 
				MenuNavi.Expand, rs.getLastRowId());
		HtmlWindow win = new HtmlDiv();
		win.bind(script);
		return win;
	}

	/**
	 *  修改菜单提交
	 * @return 刷新脚本
	 */
	public HtmlWindow editSubmit() throws ErrorMessage{
		String menuName = params.getByKey(MenuStruc.MENU_NAME);
		String menuDesc = params.getByKey(MenuStruc.MENU_DESC);
		String menuPicPath = params.getByKey(MenuStruc.PIC_PATH);
		String menuActionId = params.getByKey(MenuStruc.ACTION_ID);
		MenuManageBean menuOpt = MenuManageBean.getInstance(mUser.getUserID());
		//修改操作
		menuOpt.update(recordId, menuName, menuDesc, menuPicPath, menuActionId);
		
		//修改后刷新树
		String script = ScriptTreeNew.getRefreshModify(params.getByKey(TreeParam.NODE_CONTARINER_ID), 
				MenuNavi.Expand);
		HtmlWindow win = new HtmlDiv();
		win.bind(script);
		return win;
	}
	
	/**
	 *  菜单信息编辑界面
	 */
	private HtmlWindow panel(UserInfo mUser,String sHomeUrl,String recordId,String pageId,
			InputPrompt editComp,boolean edit) throws ErrorMessage{
		String name = null;
		String desc = null;
		String path = null;
		String action = null;
		OperatorResult menuInfo = Operator.get(MenuOptCode.Query_By_ID, mUser.getUserID(), 
				new DataFilter(recordId));
		if(TOOLS.exist(menuInfo) && edit){
			name = menuInfo.getValue(MenuStruc.MENU_NAME);
			desc = menuInfo.getValue(MenuStruc.MENU_DESC);
			path = menuInfo.getValue(MenuStruc.PIC_PATH);
			action = menuInfo.getValue(MenuStruc.ACTION_ID);
		}
		//放入菜单信息
		editComp.putTh("名称", 0, 0);
		editComp.putTd(new Input(null,MenuStruc.MENU_NAME,name,30,50).get(), 0, 1);
		editComp.putTh("说明", 1, 0);
		editComp.putTd(new TextArea(null,MenuStruc.MENU_DESC,desc,28,5).get(), 1, 1);
		editComp.putTh("图标路径", 2, 0);
		editComp.putTd(new Input(null,MenuStruc.PIC_PATH,path,30,100).get(), 2, 1);
		editComp.putTh("跳转动作ID", 3, 0);
		editComp.putTd(new Input(null,MenuStruc.ACTION_ID,action,30,100).get(), 3, 1);
		//设置检查项
		editComp.setColSize(3, 0, 12);
		editComp.setWidth("300");
		editComp.putCheckItem(MenuStruc.MENU_NAME, "菜单名称不能为空!");
		return editComp.get();
	}
}
